package com.bjsxt.shop.back.springmvc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.goods.service.IStatService;

@Controller
@RequestMapping("/back/stat")
public class StatBackController extends BaseController
{
	@Resource
	private IStatService statService;
	/**
	 * 商品列表(统计)
	 * @return
	 */
	@RequestMapping("/goodsList")
	public String goodsList(String st,String ed,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=====goodsList=====");
		/* 取数据 */
		this.goodsListJSON(st, ed, model);
		return "/back/stat/goodList";
	}
	
	/**
	 * easyui 要求,必须提供一个接口,用来获取数据
	 * @param st
	 * @param ed
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/goodsListJSON",produces="text/html;charset=UTF-8")
	public String goodsListJSON(String st,String ed,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=====goodsListJSON=====");
		JSONObject resultJSON = new JSONObject();
		if(st== null)
		{
			st="";
		}
		if(ed== null)
		{
			ed="";
		}
		Date stDate = null;
		Date edDate = null;
		if(!"".equalsIgnoreCase(st)&& !"".equalsIgnoreCase(ed) )
		{
			stDate = this.dateFormatUtil.strDateTime(st);
			edDate = this.dateFormatUtil.strDateTime(ed);
		}
		Map<String,Object> condMap = new HashMap<String,Object>();
		/* 需要两个时间 */
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		
		List<Map> statList = this.statService.statGoodsMapService(condMap);
		/* 存储结果 */
		model.addAttribute("statList", statList);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		
		resultJSON.put("total", statList.size());
		
		JSONArray rowsArr = new JSONArray();
		/* 拼装json */
		for (Iterator iterator = statList.iterator(); iterator.hasNext();)
		{
			Map map = (Map) iterator.next();
			/* 数据项 */
			JSONObject rowJSON = new JSONObject();
			rowJSON.put("createTime", map.get("createTime"));
			rowJSON.put("count", map.get("count"));
			
			rowsArr.add(rowJSON);
		}
		resultJSON.put("rows", rowsArr);
		return resultJSON.toJSONString();
	}
	
}
